package edu.utd.text.game.domain.player;

import java.util.ArrayList;

import edu.utd.text.game.domain.exceptions.GameObjectNotFoundException;
import edu.utd.text.game.domain.exceptions.ItemAlreadyExistsInInventoryException;
import edu.utd.text.game.domain.item.GameItem;

public class Inventory {
	private ArrayList<GameItem> items;
	
	public Inventory() {
		items = new ArrayList<GameItem>();
	}
	
	public Inventory(ArrayList<GameItem> items) {
		this.items = items;
	}
	
	public Inventory(GameItem singleItem) {
		items = new ArrayList<GameItem>();
		items.add(singleItem);
	}
	
	public void addItem(GameItem itemToAdd)
		throws	ItemAlreadyExistsInInventoryException
	{
		if(items.contains(itemToAdd))
			throw new ItemAlreadyExistsInInventoryException();
		
		this.items.add(itemToAdd); 
	}
	
	public boolean removeItem(GameItem item) {
		return this.items.remove(item);
	}
	
	//TODO : Test me!
	public GameItem getItem(String itemName) 
		throws GameObjectNotFoundException
	{
		if (items != null && !items.isEmpty()) {
			for (GameItem item : items) {
				if (item.getName().equals(itemName) || item.tagsMatch(itemName)) {
					return item;
				}
			}
		}
		throw new GameObjectNotFoundException();
	}
	
	
	public int getNumberOfItems() {
		return this.items.size();
	}
	
	public boolean contains(GameItem item) {
		if (items == null) 
			return false;
		return items.contains(item);
	}
	
	public boolean contains(String itemName) {
		if(items != null) {
			for (GameItem item : items) {
				try {
					getItem(itemName);
					return true;
				} catch (GameObjectNotFoundException gonfx) {
					return false;
				}
			}
		}
		return false;
	}
	
	public boolean containsItemOfType(Class itemType) {
		for(GameItem anItem : items) {
			if(anItem.getClass() == itemType)
				return true;
		}
		return false;
	}
	
	public ArrayList<String> listInventory() {
		ArrayList<String> inventoryList = new ArrayList<String>();
		if(items != null) {
			for (GameItem item : items) {
				inventoryList.add(item.getName());
			}
		}
		return inventoryList;
	}
	
	public ArrayList<GameItem> getCopy() {
		return (ArrayList<GameItem>) items.clone();
	}
		
	//////////////////////
	// getters, setters //
	//////////////////////
	
	

}	
